package com.zm.common.utils.object;

import com.alibaba.fastjson.JSON;

import java.io.*;

/**
 * 对象处理 工具类
 *
 * @Author: 汪焰
 * @Date: 2020/6/7 22:33
 * version 1.0.0
 **/
public class ObjectUtils {

//     <!--        json -->
//        <dependency>
//            <groupId>com.alibaba</groupId>
//            <artifactId>fastjson</artifactId>
//            <version>1.2.53</version>
//        </dependency>

    public static <T> String beanToString(T data) {
        if (data == null) {
            return null;
        }
        Class<?> clazz = data.getClass();
        if (clazz == String.class) {
            return (String) data;
        } else if (clazz == int.class || clazz == Integer.class) {
            return String.valueOf(data);
        } else if (clazz == long.class || clazz == Long.class) {
            return String.valueOf(data);
        } else if (clazz == double.class || clazz == Double.class) {
            return String.valueOf(data);
        } else {
            return JSON.toJSONString(data);
        }
    }

    public static <T> T stringToBean(String data, Class<T> clazz) {
        if (data == null || data.length() <= 0 || clazz == null) {
            return null;
        }
        if (clazz == String.class) {
            return (T) data;
        } else if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(data);
        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Integer.valueOf(data);
        } else if (clazz == double.class || clazz == Double.class) {
            return (T) Integer.valueOf(data);
        } else {
            return JSON.parseObject(data, clazz);
        }
    }

    public static boolean isNull(Object obj) {
        return null == obj;
    }

    public static boolean isNotNull(Object obj) {
        return null != obj;
    }

    public static int toInteger(Object value, int defaultValue) {
        int val = defaultValue;
        try {
            val = Integer.parseInt(value.toString());
        } catch (Exception e) {
        }
        return val;
    }

    public static long toLong(Object value, long defaultValue) {
        long val = defaultValue;
        try {
            val = Long.parseLong(value.toString());
        } catch (Exception e) {
            val = defaultValue;
        }
        return val;
    }

    /**
     * 序列化
     */
    public static byte[] serializer(Object object) {
        ObjectOutputStream objectOutputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            byte[] getByte = byteArrayOutputStream.toByteArray();
            return getByte;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 反序列化
     */
    public static Object deserializer(byte[] binaryByte) {
        ObjectInputStream objectInputStream = null;
        ByteArrayInputStream byteArrayInputStream = null;
        byteArrayInputStream = new ByteArrayInputStream(binaryByte);
        try {
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return objectInputStream.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}